package com.demo.security.access.method;

import java.util.Collection;

import com.demo.security.access.LogicAttribute;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.AopProxyUtils;

/**
 * 抽象方法逻辑元数据配置资源
 * 
 * @author ryuu.kk
 */
public abstract class AbstractMethodLogicMetadataSource implements MethodLogicMetadataSource {
	/**
	 * log日志
	 */
    protected final Log logger = LogFactory.getLog(getClass());
    
    public final Collection<LogicAttribute> getAttributes(Object object) {
        if (object instanceof MethodInvocation) {
            MethodInvocation mi = (MethodInvocation) object;
            Object target = mi.getThis();
            Class<?> targetClass = null;

            if (target != null) {
                targetClass = target instanceof Class<?> ? (Class<?>)target : AopProxyUtils.ultimateTargetClass(target);
            }

            return getAttributes(mi.getMethod(), targetClass);
        }

        throw new IllegalArgumentException("Object must be a non-null MethodInvocation");
    }
	
	public final boolean supports(Class<?> clazz) {
		return (MethodInvocation.class.isAssignableFrom(clazz));
	}
}
